<?php
include_once 'plugin/ExpressTactics.php';
class serviceModel extends commonModel {


    public function __construct() {
        $this->initialDB();
    }

    # 指派 门店编号+运单编号+指派ID
    public function send_order($order){
        $config = Config::getInstance();
        $website_array = $config->getWebSiteConfig();
        $sendorder_result=array("result_code"=>"-1","express_code"=>'-1',"express_code_desc"=>'接口失败');
        $log=new Logging();
        $log->lfile('push_express_order');
        foreach($order as $k=>$v){
            $data_id = $this->addExpressData($v, 2);  //建立指派订单
            $EXP = new GetExpressTactics();
            $res = $EXP->get_result($website_array['debug'],$v,$log,$sendorder_result,$this);
            if($res['status'] == 200){
                $this->push_success($res['result'],$res['info']);
            }else{
                $this->push_fail($res['result'],$res['info']);
            }
            $this->updateExpressData($data_id, $res['info']);
        }
    }


    public function push_success($_result, $info){

        if(!empty($_result['express_order_no'])){  # 订单创建成功
            $where = array("AND" => array("=" => array('shop_express_no' => $_result['shop_express_no'], "status"=>1,"isdeleted"=>0)));
            $orderInfo = $this->getItemByOther($where,'orders_express');
            if(!empty($orderInfo)){
                //更新指派订单和订单的状态
                $columns=array();
                $columns['push_response_data'] =  json_encode($info,JSON_UNESCAPED_UNICODE);
                $columns['express_order_no'] = $_result['express_order_no'];
                $columns['express_fee'] = $_result['express_fee'];
                $columns['status'] = 2;

                $OrderColumns=array();
                $OrderColumns['status'] = 2;
                $where = array("AND" => array("=" => array("ph_no"=>$orderInfo[0]['ph_no'],"status" => 1,"isdeleted" => 0)));
                $this->updateItem($OrderColumns,$where,'orders');
            }else {
                $columns['status'] = 0;  //推送失敗
                $columns['push_response_data'] =  json_encode($_result,JSON_UNESCAPED_UNICODE);

            }

            $where = array("AND" => array("=" => array("shop_express_no"=>$_result['shop_express_no'],"status" => 1,"isdeleted" => 0)));
            $this->updateItem($columns,$where,'orders_express');
        }

    }


    public function push_fail($_result, $info){
            $columns=array();
        $columns['status'] = 0;  //推送失敗
        $columns['push_response_data'] = json_encode($info,JSON_UNESCAPED_UNICODE);

        $where = array("AND" => array("=" => array("shop_express_no"=>$_result['shop_express_no'],"status" => 1,"isdeleted" => 0)));
        $this->updateItem($columns,$where,'orders_express');
    }



    # 仅推送二天内的订单给快递
    public function GetExpressOrders() {
        $shop_no = $_SESSION['zp_shop']['store_id'];
        $lastDay = date('Y-m-d H:i:s',strtotime('+2 day'));
        $sql = " SELECT * FROM orders_express WHERE isdeleted = 0 AND status = 1 AND shop_no = $shop_no AND shop_pickup_time <= '$lastDay'";
//        $sql = " SELECT * FROM orders_express WHERE isdeleted = 0 AND status = 1 AND shop_no = $shop_no AND express_type = 'jrd' AND shop_pickup_time <= '$lastDay'";
        $this->db->setStatement($sql);
        $result= $this->db->execute();
        $result=$this->getArray($result);
        return $result;
    }

    public function getToken(){
        $sql = " SELECT * FROM `access_token` WHERE `type`='slw_token' ";
        $this->db->setStatement($sql);
        $result= $this->db->execute();
        $result=$this->getArray($result,'one');
        return $result;
    }
}